package cn.bluarry.sqlitetest;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyDataBaseHelper extends SQLiteOpenHelper {
    private String TAG="MyDataBaseHelper";


    public static final String Create_table="CREATE TABLE Book(" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "author TEXT," +
            "price REAL," +
            "pages INTEGER," +
            "name TEDT);";
    public static final String Create_cate="CREATE TABLE Category(" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "category_name TEXT," +
            "category_code TEXT);";

    private Context mContext;

    public MyDataBaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext=context;
        Log.d(TAG, "构造函数  ");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(Create_table);
        db.execSQL(Create_cate);
        Toast.makeText(mContext,"数据库创建成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS Book;");
        db.execSQL("DROP TABLE IF EXISTS Category;");
        onCreate(db);
    }
}
